/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TranferirDocGUI.java
 *
 * Created on 13/09/2012, 02:31:13 AM
 */
package Gui;

import ClassControl.CtrTraslado;
import ClassEntidad.ActaTraslado;
import ClassEntidad.Dependencia;
import ClassEntidad.Documento;
import ClassEntidad.Sistema;
import ClassEntidad.TRD;
import Servicios.DependenciaService;
import Servicios.DocumentoService;
import Servicios.TRDService;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import util.CparaCombo;

/**
 *
 * @author borisgr04
 */
public class TranferirDocGUI extends javax.swing.JFrame implements Observer{

    CtrTraslado c = new CtrTraslado(Sistema.instancia().getEmf());
    List<Documento> ld;
    private TablaBandejaArchivados modeloTabla;

    /**
     * Creates new form TranferirDocGUI
     */
    public TranferirDocGUI() {
        initComponents();
        inicializar();
        //this.fechaJC.getCalendar().setTime(new Date());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        serieC = new javax.swing.JComboBox();
        depOrigenC = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        generarActaB = new javax.swing.JButton();
        consultaB = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        fechaJC = new com.toedter.calendar.JDateChooser();

        jLabel12.setText("Dependencia Origen");

        jLabel13.setText("Serie Documental");

        depOrigenC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                depOrigenCActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setText("TRANFERENCIA A ARCHIVO CENTRAL");

        jLabel1.setText("Fecha de Corte");

        generarActaB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/report-icon.png"))); // NOI18N
        generarActaB.setText("Generar Acta");
        generarActaB.setEnabled(false);
        generarActaB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generarActaBActionPerformed(evt);
            }
        });

        consultaB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/search-b-icon.png"))); // NOI18N
        consultaB.setText("Consultar");
        consultaB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultaBActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel13))
                        .addGap(42, 42, 42)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(depOrigenC, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(serieC, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fechaJC, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addComponent(consultaB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(generarActaB)
                        .addGap(99, 99, 99))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 621, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel12)
                    .addContainerGap(615, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(depOrigenC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13)
                            .addComponent(serieC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(consultaB)
                        .addComponent(generarActaB)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(fechaJC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(58, 58, 58)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addComponent(jLabel12)
                    .addContainerGap(309, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 647, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void depOrigenCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_depOrigenCActionPerformed
        // TODO add your handling code here:
        this.InicializaTRD();
    }//GEN-LAST:event_depOrigenCActionPerformed

    public void InicializarB() {
        //enviadosJT
        
    }
    private void consultaBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultaBActionPerformed
        try {
        
            ActGrd();
            if (ld.size() > 0) {
                this.generarActaB.setEnabled(true);
            } else {
                JOptionPane.showMessageDialog(this, "No se encontraron Documentos", Sistema.instancia().getNomApp(), JOptionPane.INFORMATION_MESSAGE);
                this.generarActaB.setEnabled(false);
            }
        } catch (NumberFormatException exX) {
            JOptionPane.showMessageDialog(this, "Valor de Número de Acta No Válido", Sistema.instancia().getNomApp(), JOptionPane.INFORMATION_MESSAGE);
            this.generarActaB.setEnabled(false);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), Sistema.instancia().getNomApp(), JOptionPane.INFORMATION_MESSAGE);
            this.generarActaB.setEnabled(false);
        }


    }//GEN-LAST:event_consultaBActionPerformed

    private void generarActaBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generarActaBActionPerformed
        // TODO add your handling code here:
        try {
            ActaTraslado at = new ActaTraslado();
            if (this.fechaJC.getCalendar().toString().trim().isEmpty()) {
                JOptionPane.showMessageDialog(this, "Escoja una Fecha", Sistema.instancia().getNomApp(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            at.setFecActa(this.fechaJC.getCalendar().getTime());
            at.setDocActas(ld);
            CparaCombo depOrg = (CparaCombo) depOrigenC.getSelectedItem();
            if (depOrg == null) {
                JOptionPane.showMessageDialog(this, "Seleccione Dependencia Origen", Sistema.instancia().getNomApp(), JOptionPane.WARNING_MESSAGE);
                return;
            }
            c.setDep(depOrg.getCodigo());
            c.Guardar(at);
            if (c.isValido()) {
                                
                JOptionPane.showMessageDialog(this, "Se Generó el Acta N° " + at.getNroActa(), Sistema.instancia().getNomApp(), JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(true);

            } else {
                JOptionPane.showMessageDialog(this, c.getMensaje(), Sistema.instancia().getNomApp(), JOptionPane.WARNING_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), Sistema.instancia().getNomApp(), JOptionPane.WARNING_MESSAGE);
        }

    }//GEN-LAST:event_generarActaBActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TranferirDocGUI().setVisible(true);
            }
        });
    }

    private void InicializaDependencias() {
        //Llena Comobo Box Dependencia
        List<Dependencia> lstDep = Sistema.instancia().getDependencias();
        for (Dependencia d : lstDep) {
            this.depOrigenC.addItem(new CparaCombo(d.getId(), d.getNombre()));
        }
    }

    private void InicializaTRD() {
        //Llena Comobo Box Serie
        //Inicialziando
        serieC.removeAllItems();
        Dependencia d = DepActual();
        if (!(d == null)) {
            int i = 0;
            for (TRD serie : d.gettRDs()) {
                //if (i > 0) {//ojo la primera serie es la serie Externa
                this.serieC.addItem(new CparaCombo(String.valueOf(serie.getId()), serie.getSerie().toUpperCase()));
                //}
                i++;
                //System.out.print(serie.getSerie());
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton consultaB;
    private javax.swing.JComboBox depOrigenC;
    private com.toedter.calendar.JDateChooser fechaJC;
    private javax.swing.JButton generarActaB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JComboBox serieC;
    // End of variables declaration//GEN-END:variables

    private void inicializar() {
        this.InicializaDependencias();
        this.InicializaTRD();
        
        this.setResizable(false);
        this.setLocationRelativeTo(null);
Sistema.instancia().addObserver(this);

    }

    private Dependencia DepActual() {
        DependenciaService ds = new DependenciaService(Sistema.instancia().getEmf());
        CparaCombo dpOr = (CparaCombo) depOrigenC.getSelectedItem();
        Dependencia d = ds.findDependencia(dpOr.getCodigo());
        return d;
    }

    private TRD SerieActual() {
        TRDService ss = new TRDService(Sistema.instancia().getEmf());
        CparaCombo dpSr = (CparaCombo) this.serieC.getSelectedItem();
        TRD t = ss.findTRD(Long.valueOf(dpSr.getCodigo()));
        return t;
    }

    private void ActGrd() {
            DocumentoService ds = new DocumentoService(Sistema.instancia().getEmf());
            TRD serie = this.SerieActual();
        Calendar fecha = this.fechaJC.getCalendar();
        fecha.set(Calendar.YEAR, fecha.get(Calendar.YEAR) - serie.gettRetAG());
        Date dfecha = fecha.getTime();
        ld = ds.findxTrasladar(this.SerieActual(), dfecha);
        modeloTabla = new TablaBandejaArchivados();
        modeloTabla.setLstdoc(ld);
        this.jTable1.setModel(modeloTabla);
     ;
    }

    @Override
    public void update(Observable o, Object arg) {
        ActGrd();
    }
}
